$(function() {
  var a = 1;
  function public(age) {
    $.ajax({
      type: "get",
      url: "/user/queryUser",
      data: {
        pageSize: 5,
        page: age == undefined ? a : age
      },
      dataType: "json",
      success: function(res) {
        console.log(res);
        $("tbody").html(template("tmp", res));
      }
    });
  }
  public();

  var promise = new Promise(function(rest, rej) {
    $.ajax({
      type: "get",
      url: "/user/queryUser",
      data: {
        pageSize: 5,
        page: a
      },
      dataType: "json",
      success: function(res) {
        console.log(res);
        $("tbody").html(template("tmp", res));
        rest(res);
      }
    });
  });

  promise.then(function(res) {
    console.log(res);
    $(".btn-rg").click(function() {
      if (a >= Math.ceil(res.total / res.size)) {
        a = 0;
      }
      a++;
      console.log(a);
      public(a);
    });
    $(".btn-le").click(function() {
      a--;
      console.log(a);
      if (a < 1) {
        a = Math.ceil(res.total / res.size);
      }
      public(a);
    });
  });

  var ind = 1;
  function Ind() {
    $.ajax({
      type: "get",
      url: "/user/queryUser",
      data: {
        pageSize: 5,
        page: ind
      },
      dataType: "json",
      success: function(res) {
        // console.log(res);
        var str = Math.ceil(res.total / res.size);
        for (var i = 1; i <= str; i++) {
          var newli = $(
            "<span class='span1'>" +
              i +
              ' <span class="sr-only">(current)</span></span>'
          );
          $(".user-pages").append(newli);
        }
        $(".user-pages .span1").click(function() {
          var page = parseInt($(this).text());
          ind = page;
          public(page);
        });
      }
    });
  }
  Ind();

  // 顶级 禁用 或 启用 按钮  或取当前 id 值 与是否启用的值  然后点击  模态框的确认按钮向后台发送请求
  $(".us-tab").on("click", "button", function() {
    // console.log(123);
    // 获取id
    var tx = $(this)
      .parent()
      .parent()
      .attr("tx");
    var isDelete = $(this)
      .parent()
      .prev()
      .text()
      .trim();

    if (isDelete == "开启") {
      isDelete = 0;
    } else {
      isDelete = 1;
    }
    // console.log(isDelete);

    //点击确认按钮 向后台发送请求
    $(".btn-affirm1").click(function() {
      $.ajax({
        type: "post",
        url: "/user/updateUser",
        data: {
          id: tx,
          isDelete: isDelete
        },
        dataType: "json",
        success: function(res) {
          //   console.log(ind);
          public(ind);
        }
      });
    });
  });
});
